package com.weight.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import com.weight.common.model.TreeDataResult;
import lombok.extern.slf4j.Slf4j;
/**
 * 转树形List
 *
 */
@Slf4j
public class TreeNodeUtil {
	
	/**
	 * 常规List---转---树形List(easyui版)--JSON格式
	 * @param treeListJson  ObjectList的JSON格式
	 * @return
	 */
	 public final static String getTreeNodeListJSON(String treeListJson) {
		 log.info("treeListJson={}",treeListJson);
		 	List<TreeDataResult> resultTreeDataList=getTreeNodeList(treeListJson);
	        return JSON.toJSONString(resultTreeDataList);
	 }
	
	/**
	 * 常规List---转---树形List(easyui版)
	 * @param treeListJson  ObjectList的JSON格式
	 * @return
	 */
    public final static List<TreeDataResult> getTreeNodeList(String treeListJson) {
    	//1.格式转换为TreeDataResult模板
    	List<TreeDataResult> treeDataList=JSON.parseArray(treeListJson, TreeDataResult.class);
//    	 log.info("treeDataList={}",treeDataList);
//    	 log.info("treeDataList.size={}",treeDataList.size());
    	 
    	//2.定义返回 List
        List<TreeDataResult> resultTreeDataList = new ArrayList<TreeDataResult>();
        //3.返回List拼装
        for (TreeDataResult treeDataResult : treeDataList) {
        	if(StringUtils.isBlank(treeDataResult.getPid()))continue;
        	
        	 //4.获取父节点
            if(treeDataResult.getPid().equals("0")) {
            	//5.设置子节点----根据父节点ID，获取子节点
            	treeDataResult.setChildren(getChildrenNode(treeDataResult.getId(),treeDataList));
            	treeDataResult.setState("open");
                
                resultTreeDataList.add(treeDataResult);
            }
        }
        return resultTreeDataList;
    }
    
	/**
	 * 根据父节点ID，获取子节点
	 * @param pid	父节点ID
	 * @param treeDataList
	 * @return
	 */
    private final static List<TreeDataResult> getChildrenNode(String pid , List<TreeDataResult> treeDataList) {
    	//1.定义返回 List
        List<TreeDataResult> resultTreeDataList = new ArrayList<TreeDataResult>();
        //2.返回List拼装
        for (TreeDataResult TreeDataResult : treeDataList) {
            if(TreeDataResult.getPid() == null)  continue;
            //3.获取 子节点
            if(TreeDataResult.getPid().equals(pid)){
            	//4.设置子节点下的子节点----循环递归 
                TreeDataResult.setChildren(getChildrenNode(TreeDataResult.getId() , treeDataList));
                resultTreeDataList.add(TreeDataResult);
            }
        }
        return resultTreeDataList;
    }
	/**
	 *  树形 
	 */
	 public final static String getTreeListJSON(String treeListJson,boolean hasHeader) {
		
	 	 List<TreeDataResult> resultTreeDataList=getTreeNodeListNorm(treeListJson,hasHeader);
         return JSON.toJSONString(resultTreeDataList);
	 }
	 public final static List<TreeDataResult> getTreeNodeListNorm(String treeListJson,boolean hasHeader) {
		 log.info("treeListJson={}",treeListJson,hasHeader);

    	 List<TreeDataResult> treeDataList=JSON.parseArray(treeListJson, TreeDataResult.class);
    	 if(hasHeader&&CollectionUtils.isNotEmpty(treeDataList)) {
    		 List<TreeDataResult> treeDataLists=new ArrayList<TreeDataResult>();
//    		 treeDataLists.add(new TreeDataResult("","全部"));    		 
    		 TreeDataResult treeDataResult=new TreeDataResult("","全部");
    		 treeDataResult.setChildren(treeDataList);
    		 treeDataLists.add(treeDataResult);
//    		 CollectionUtils.addAll(treeDataLists,treeDataList);
    		 log.info("treeDataLists={}",treeDataLists,hasHeader);
    		 return treeDataLists;
    	 }
    	 return treeDataList;
	 } 
}